package com.john.excel.create;

import com.alibaba.fastjson.JSONObject;

import java.util.*;

/**
 * xlsx省市区级联数据
 * @author john
 */
public class ProvinceUtil {
	//省市区 json 信息
	private static StringBuffer provinceList = new StringBuffer();
	
	/**
	 * 添加省
	 * @param parmList	数据集合
	 * @param cellIndex	列下标 1开始
	 * @param level 下拉级联时，父级的位置 从1开始   如果给1，表示从A列开始，2，就是从B列
	 * @param selectCellAttr 下拉列表属性
	 * @param cascade 是否级联
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static void getProvince(List<SelectCellAttr> parmList,Integer cellIndex,Integer level,SelectCellAttr selectCellAttr,boolean cascade){
		try {
			List<String> province = new ArrayList<String>();
			JSONObject jsonObject = JSONObject.parseObject(provinceList.toString());
			Iterator iterator = jsonObject.keySet().iterator();
			while (iterator.hasNext()){
				String key = (String) iterator.next();
				province.add(jsonObject.getJSONObject(key).getString("name"));
			}
			SelectCellAttr attr = new SelectCellAttr();
			attr.setAlignment(selectCellAttr.getAlignment());
			attr.setBackgroundColor(selectCellAttr.getbackgroundColor());
			attr.setBorder(selectCellAttr.getBorder());
			attr.setBorderColor(selectCellAttr.getBorderColor());
			attr.setCellWith(selectCellAttr.getCellWith());
			attr.setFontColor(selectCellAttr.getFontColor());
			attr.setPromptType(selectCellAttr.getPromptType());
			attr.setRowHeight(Integer.valueOf(selectCellAttr.getRowHeight()));
			attr.setVerticalAlignment(selectCellAttr.getVerticalAlignment());
			attr.setVlaue(selectCellAttr.getVlaue());
			attr.setWrapText(selectCellAttr.getWrapText());
			parmList.add(attr.setSelectCellAttr("省列表", cellIndex, level, Arrays.copyOf(province.toArray(),province.toArray().length,String[].class), cascade));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加市
	 * @param parmList	数据集合
	 * @param cellIndex	列下标 1开始
	 * @param level 下拉级联时，父级的位置 从1开始   如果给1，表示从A列开始，2，就是从B列
	 * @param selectCellAttr 错误或输入提示提示
	 * @param cascade 是否级联
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static void getCity(List<SelectCellAttr> parmList,Integer cellIndex,Integer level,SelectCellAttr selectCellAttr,boolean cascade){
		try {
			Map<String,String[]> map = new HashMap<String,String[]>();
			JSONObject jsonObject = JSONObject.parseObject(provinceList.toString());
			Iterator iterator = jsonObject.keySet().iterator();
			while (iterator.hasNext()){
				String key = (String) iterator.next();
				String[] data = getDatas(jsonObject.getJSONObject(key).getJSONObject("child"),"name");
				if(data.length > 0){
					map.put(jsonObject.getJSONObject(key).getString("name"), data);
				}else{
					map.put(jsonObject.getJSONObject(key).getString("name"), new String[]{jsonObject.getJSONObject(key).getString("name").replace("省", "")});
				}
			}
			Iterator<String>  keyIterator = map.keySet().iterator();  
		    while(keyIterator.hasNext()){  
		        String key = keyIterator.next();  
		        String[] son = map.get(key);
		        SelectCellAttr attr = new SelectCellAttr();
				attr.setAlignment(selectCellAttr.getAlignment());
				attr.setBackgroundColor(selectCellAttr.getbackgroundColor());
				attr.setBorder(selectCellAttr.getBorder());
				attr.setBorderColor(selectCellAttr.getBorderColor());
				attr.setCellWith(selectCellAttr.getCellWith());
				attr.setFontColor(selectCellAttr.getFontColor());
				attr.setPromptType(selectCellAttr.getPromptType());
				attr.setRowHeight(Integer.valueOf(selectCellAttr.getRowHeight()));
				attr.setVerticalAlignment(selectCellAttr.getVerticalAlignment());
				attr.setVlaue(selectCellAttr.getVlaue());
				attr.setWrapText(selectCellAttr.getWrapText());
				parmList.add(attr.setSelectCellAttr(key, cellIndex,level,son,cascade));
		    }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 添加区
	 * @param parmList	数据集合
	 * @param cellIndex	列下标 1开始
	 * @param level 下拉级联时，父级的位置 从1开始   如果给1，表示从A列开始，2，就是从B列
	 * @param selectCellAttr 错误或输入提示提示
	 * @param cascade 是否级联
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static void getArea(List<SelectCellAttr> parmList,Integer cellIndex,Integer level,SelectCellAttr selectCellAttr,boolean cascade){
		try {
			Map<String,String[]> map = new HashMap<String,String[]>();
			JSONObject jsonObject = JSONObject.parseObject(provinceList.toString());
			Iterator iterator = jsonObject.keySet().iterator();
			while (iterator.hasNext()){
				String key = (String) iterator.next();
				JSONObject jsonObject1 = jsonObject.getJSONObject(key).getJSONObject("child");
				Iterator iterator1 = jsonObject1.keySet().iterator();
				while (iterator1.hasNext()){
					String keys = (String) iterator1.next();
					if(!jsonObject1.getJSONObject(keys).getString("name").contains("县")){
						Iterator iterator2 = jsonObject1.getJSONObject(keys).getJSONObject("child").keySet().iterator();
						List<String> province = new ArrayList<String>();
						while(iterator2.hasNext()){
							String keys1 = (String) iterator2.next();
							province.add(jsonObject1.getJSONObject(keys).getJSONObject("child").getString(keys1));
						}
						map.put(jsonObject1.getJSONObject(keys).getString("name"),Arrays.copyOf(province.toArray(),province.toArray().length,String[].class));
					}
				}
			}
			Iterator<String>  keyIterator = map.keySet().iterator();  
		    while(keyIterator.hasNext()){  
		        String key = keyIterator.next();  
		        String[] son = map.get(key);
		        SelectCellAttr attr = new SelectCellAttr();
				attr.setAlignment(selectCellAttr.getAlignment());
				attr.setBackgroundColor(selectCellAttr.getbackgroundColor());
				attr.setBorder(selectCellAttr.getBorder());
				attr.setBorderColor(selectCellAttr.getBorderColor());
				attr.setCellWith(selectCellAttr.getCellWith());
				attr.setFontColor(selectCellAttr.getFontColor());
				attr.setPromptType(selectCellAttr.getPromptType());
				attr.setRowHeight(Integer.valueOf(selectCellAttr.getRowHeight()));
				attr.setVerticalAlignment(selectCellAttr.getVerticalAlignment());
				attr.setVlaue(selectCellAttr.getVlaue());
				attr.setWrapText(selectCellAttr.getWrapText());
				parmList.add(attr.setSelectCellAttr(key, cellIndex,level,son,cascade));
		    }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取数据
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private static String[] getDatas(JSONObject jsonObject,String key){
		try {
			List<String> province = new ArrayList<String>();
			Iterator iterator = jsonObject.keySet().iterator();
			while (iterator.hasNext()){
				String keys = (String) iterator.next();
				if(!jsonObject.getJSONObject(keys).getString(key).contains("县")){
					province.add(jsonObject.getJSONObject(keys).getString(key));
				}
			}
			return Arrays.copyOf(province.toArray(),province.toArray().length,String[].class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
